package cn.com.mayue.guava.caches;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/22.
 */
public class CacheTest {
    static Map<String, Integer> map = Maps.newConcurrentMap();

    public static Integer getValueByKey(String key) {
//        System.out.println("通过key：" + key + "获取值");
        return map.get(key);
    }

    @Test
    public void loadingCache() throws Exception {
        map.put("key1", 1);
        map.put("key2", 2);
        map.put("key3", 3);
        LoadingCache<String, Integer> cahceBuilder =
                CacheBuilder.newBuilder().maximumSize(3)
                        .build(new CacheLoader<String, Integer>() {
                            @Override
                            public Integer load(String key) throws Exception {
                                return getValueByKey(key);
                            }

                        });
        System.out.println(cahceBuilder.get("key1"));
        System.out.println(cahceBuilder.get("key2"));
        System.out.println(cahceBuilder.get("key3"));

        System.out.println(cahceBuilder.get("key1"));
        System.out.println(cahceBuilder.get("key2"));
        System.out.println(cahceBuilder.get("key3"));
        cahceBuilder.put("key1", 4);
        System.out.println(cahceBuilder.get("key1"));
    }

}
